//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package atomgit_triger

import (
	"encoding/json"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/triger_impl/core"
)

type PushHookInfo struct {
	Before     string         `json:"before"`
	After      string         `json:"after"`
	Ref        string         `json:"ref"`
	Repository RepositoryInfo `json:"repository"`
	Sender     SenderInfo     `json:"sender"`
}

func ExtractPushHookAttr(data []byte) ([]string, error) {
	obj := &PushHookInfo{}

	err := json.Unmarshal(data, obj)
	if err != nil {
		return nil, err
	}

	retList := []string{
		fmt.Sprintf("%s=%s", core.GIT_HOOK_TYPE, "push"),
		fmt.Sprintf("%s=%s", core.GIT_NAME, obj.Repository.Name),
		fmt.Sprintf("%s=%s", core.GIT_WEB_URL, obj.Repository.HtmlUrl),
		fmt.Sprintf("%s=%s", core.GIT_HTTP_URL, obj.Repository.CloneUrl),
		fmt.Sprintf("%s=%s", core.GIT_SSH_URL, obj.Repository.SshUrl),
		fmt.Sprintf("%s=%s", core.GIT_USERNAME, obj.Sender.Login),

		fmt.Sprintf("%s=%s", core.GIT_PUSH_BEFORE, obj.Before),
		fmt.Sprintf("%s=%s", core.GIT_PUSH_AFTER, obj.After),
		fmt.Sprintf("%s=%s", core.GIT_PUSH_REF, obj.Ref),
	}
	return retList, nil
}
